package com.cxyzy.commonviewholderlib

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView

/**
 * 统一adapter基类
 */
abstract class BaseCommonAdapter(private val mContext: Context, private val data: List<*>) : RecyclerView.Adapter<ViewHolder>() {

    override fun onCreateViewHolder(viewGroup: ViewGroup, layoutType: Int): ViewHolder {
        val inflater = LayoutInflater.from(viewGroup.context)
        return ViewHolder(mContext, inflater.inflate(getLayoutIdByType(layoutType), viewGroup, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        data[position]?.let { onBindViewHolder(holder, getItemViewType(position), it, position) }
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    protected abstract fun getLayoutIdByType(viewType: Int): Int

    abstract override fun getItemViewType(position: Int): Int

    protected abstract fun onBindViewHolder(holder: ViewHolder, type: Int, data: Any, position: Int)
}